const state = {
  info: null,
  card: null,
  address: {}
}

const mutations = {
  updateUserInfo(state, data) {
    state.info = data;
  },
  updateCardInfo(state, data) {
    state.card = data;
  },
  updateAddress(state, {address, detail}) {
    state.address[address] = detail;
  }
}

const actions = {
  updateUserInfo({commit}, data) {
    commit('updateUserInfo', data)
  },
  updateAddress({commit, state}, data) {
    commit('updateAddress', data)
  },
  updateCardInfo({commit}, data) {
    commit('updateCardInfo', data)
  },
  logout({commit, state}) {
    commit('updateUserInfo', {token: null, ...state})
  }
}

// getters are functions
const getters = {
  token: state => (!!state.info && state.info.token),
  userInfo: state => state.info,
  cardInfo: state => state.card,
  addressPositions: state => state.address,
}

// A Vuex instance is created by combining the state, mutations, actions,
// and getters.
export default {
  state,
  getters,
  actions,
  mutations
}
